package org.example.controller;

import org.example.entity.Schedule;
import org.example.service.ScheduleService;
import org.example.util.PageBean;
import org.example.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

// @CrossOrigin 注释在带注释的控制器方法上启用跨源请求
@CrossOrigin
@RestController
@RequestMapping("schedule")
public class ScheduleController {

    @Autowired
    private ScheduleService scheduleService;

    @GetMapping("/{pageSize}/{currentPage}")
    public R showList(@PathVariable(name = "pageSize") int pageSize, @PathVariable(name = "currentPage") int currentPage) {
        PageBean<Schedule> pageBean = this.scheduleService.queryAllByPage(pageSize, currentPage);
        return R.ok(pageBean);
    }

    @PostMapping
    public R saveSchedule(@RequestBody Schedule schedule) {
        this.scheduleService.saveSchedule(schedule);
        return R.ok(null);
    }

    @DeleteMapping("/{id}")
    public R removeSchedule(@PathVariable Integer id) {
        this.scheduleService.removeById(id);
        return R.ok(null);
    }

    @PutMapping
    public R changeSchedule(@RequestBody Schedule schedule) {
        this.scheduleService.updateSchedule(schedule);
        return R.ok(null);
    }
}    